package net.j4love.zookeeper.quickstart;

import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.ZooDefs;
import org.apache.zookeeper.ZooKeeper;
import org.apache.zookeeper.data.Stat;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;

/**
 * @author He Peng
 * @create 2017-05-15 13:42
 * @update 2017-05-15 13:42
 * @see
 */
public class SyncCreateZNode {

    private static final Logger LOG = LoggerFactory.getLogger(SyncCreateZNode.class);

    public static void main(String[] args) throws IOException, KeeperException, InterruptedException {

        ZooKeeper zk = new ZooKeeper("127.0.0.1:2181", 1000, null);
        Stat stat = zk.exists("/hello_zk", null);
        if (stat == null){

            // 同步创建一个持久化节点
            zk.create("/hello_zk","hello world!".getBytes(),
                    ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
        }

        byte[] dataByte = zk.getData("/hello_zk", null, null);
        String dataStr = new String(dataByte, "UTF-8");

        LOG.debug("/test node data {} " , dataStr);

        zk.close();

    }
}
